package com.server.shop;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface ShopDao {
	
	/**
	 * 查询广告
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getBanner(Map<String, Object> map);

	/**
	 * 查询公告列表
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getNotice(Map<String, Object> map);
	
	/**
	 * 查询公告详情
	 * @param id
	 * @return
	 */
	@Select("select * from notice where id= #{id}")
	public Map<String, Object> getNoticeDetail(@Param("id")String id);

	/**
	 * 查询商品
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getGoods(Map<String, Object> map);
	
	/**
	 * 查询商品详情
	 * @param id
	 * @return
	 */
	@Select("select * from goods where id= #{id}")
	public Map<String, Object> getGoodsDetail(@Param("id")String id);
	
	/**
	 * 查询商品规格，以及对应得库存
	 * @param id
	 * @return
	 */
	@Select("select * from stock s where s.goodsid=#{id}")
	public List<Map<String, Object>> getGoodsStock(@Param("id")String id);
	
	/**
	 * 查询规格表得详情
	 * @param id
	 * @return
	 */
	@Select("select s.*,g.type goodstype from stock s left join goods g on s.goodsid = g.id  where s.id=#{id} ")
	public Map<String, Object> getStockDetail(@Param("id")String id);
	
	/**
	 * 获取所有商品分类
	 * @return
	 */
	@Select("select * from classes")
	public List<Map<String, Object>> getGoodsClass();
	
	/**
	 * 查询某位置商品
	 * @param position
	 * @return
	 */
	@Select("select g.* from positiongoods pg left join goods g on pg.goodsid=g.id  ")
	public List<Map<String, Object>> getPositionGoods(@Param("position")String position);
	
	/**
	 * 增加商品到购物车
	 * @param id
	 * @param userid
	 * @param goodsid
	 * @param stockid
	 * @param num
	 */
	@Update("call addCar(#{userid},#{goodsid},#{stockid},#{num},#{id})")
	public void addShopCar(@Param("id")String id,@Param("userid")String userid,@Param("goodsid")String goodsid,@Param("stockid")String stockid,@Param("num")int num);
	
	/**
	 * 根据关键信息，查询购物车信息
	 * @param userid
	 * @param goodsid
	 * @param stockid
	 * @return
	 */
	@Select("select * from shopcar sc where sc.userid=#{userid} and sc.goodsid=#{goodsid} and sc.stockid=#{stockid}  ")
	public Map<String, Object> getShopCarDetail(@Param("userid")String userid,@Param("goodsid")String goodsid,@Param("stockid")String stockid);
	
	/**
	 * 查询购物车某条信息
	 * @param id
	 * @return
	 */
	@Select("select * from shopcar sc where sc.id=#{id}")
	public Map<String, Object> getCarDetail(@Param("id")String id);

	
	/**
	 * 查询用户的购物车
	 * @param userid
	 * @return
	 */
	@Select("select sc.id,sc.num,sc.stockid,sc.goodsid,g.name,g.title,g.headpic,s.price,g.soldnum,s.specs,s.stock,0 ifchecked from shopcar sc left join goods g on sc.goodsid=g.id left join stock s on sc.stockid=s.id where sc.userid=#{userid}")
	public List<Map<String, Object>> getUserCar(@Param("userid")String userid);
	
	
	/**
	 * 查询用户的购物车
	 * @param userid
	 * @return
	 */
	@Select("select sc.id,sc.num,sc.stockid,sc.goodsid,g.name,g.title,g.headpic,s.price,g.soldnum,g.type,s.specs,s.stock,0 ifchecked from shopcar sc left join goods g on sc.goodsid=g.id left join stock s on sc.stockid=s.id"
			+ " where sc.userid=#{userid} and sc.id in ${ids}")
	public List<Map<String, Object>> getUserCarByID(@Param("userid")String userid,@Param("ids")String ids);
	
	/**
	 * 更新购物车中的商品数量
	 * @param id
	 * @param num
	 * @return
	 */
	@Update("update shopcar sc set sc.num=#{num} where sc.id=#{id}")
	public int upCarNum(@Param("id")String id,@Param("num")int num);
	
	/**
	 * 插入订单
	 * @param id
	 * @param userid
	 * @param amount
	 * @param addrid
	 * @return
	 */
	@Insert("insert into shoplist (id,userid,amount,addrid,reward_amount,ifreward_list,goodstype)values(#{id},#{userid},#{amount},#{addrid},#{reward_amount},#{ifreward_list},#{goodstype})")
	public int insList(@Param("id")String id,@Param("userid")String userid,@Param("amount")double amount,@Param("reward_amount")double reward_amount,@Param("addrid")String addrid,@Param("ifreward_list")String ifreward_list,@Param("goodstype")String goodstype);
	
	/**
	 * 批量插入订单商品
	 * @param list
	 */
	public void insListGoods(@Param("data")List<Map<String, Object>> data);
	

	
	/**
	 * 删除购物车中的某条记录
	 * @param id
	 * @return
	 */
	@Update("delete from shopcar where id=#{id}")
	public int delCar(@Param("id")String id);

	/**
	 * 删除购物车中的多条记录
	 * @param ids
	 * @return
	 */
	@Update("delete from shopcar where id in ${ids}")
	public int delCars(@Param("ids")String ids);
	
	/**
	 * 删除购物车中的某条记录
	 * @param id
	 * @return
	 */
	@Update("delete from shopcar where id in ${id}")
	public int delCarIn(@Param("id")String id);
	
	/**
	 * 删除购物车中的某条记录
	 * @param id
	 * @return
	 */
	@Update("delete from shopcar where goodsid in ${goodsid} and userid=#{userid}")
	public int delCarByGoodsId(@Param("goodsid")String goodsid,@Param("userid")String userid);
	
	/**
	 * 根据购物车ID列表，查询对应得商品信息
	 * @param id
	 * @return
	 */
	@Select("select * from shopcar s where s.id in ${id}")
	public List<Map<String, Object>> getGoodsIDByCarids(@Param("id")String id);
	

	/**
	 * 查询所有省
	 * @return
	 */
	@Select("select * from hat_province ")
	public List<Map<String, Object>> getProvince();

	/**
	 * 查询该省得所有市
	 * @return
	 */
	@Select("select * from hat_city c where c.father=#{id}")
	public List<Map<String, Object>> getCity(@Param("id")String id);

	/**
	 * 查询该市得所有区
	 * @return
	 */
	@Select("select * from hat_area a where a.father=#{id}")
	public List<Map<String, Object>> getArea(@Param("id")String id);
	
	/**
	 * 插入预约记录
	 * @param id
	 * @param userid
	 * @param phone
	 * @param name
	 * @param day
	 * @param yytime
	 * @param goodsid
	 * @return
	 */
	@Insert("insert into appoint_log(id,userid,goodsid,day,yytime,phone,name) values(#{id},#{userid},#{goodsid},#{day},#{yytime},#{phone},#{name})")
	public int insAppointLog(@Param("id")String id,@Param("userid")String userid,@Param("phone")String phone,@Param("name")String name,@Param("day")String day,@Param("yytime")String yytime,@Param("goodsid")String goodsid);
	
	/**
	 * 查询活动列表
	 * @return
	 */
	@Select("select * from activity_log a where a.status = '1' order by a.ctime desc ")
	public List<Map<String, Object>> getActivity();
	
	/**
	 * 查询活动详情
	 * @return
	 */
	@Select("select * from activity_log where id = #{id}")
	public Map<String, Object> getActivityDetail(@Param("id")String id);
	
	/**
	 * 查询知识列表
	 * @return
	 */
	@Select("select * from knowledge k where k.status = '1' order by k.ctime desc")
	public List<Map<String, Object>> getKnowledge();
	

	/**
	 * 查询知识详情
	 * @return
	 */
	@Select("select * from knowledge where id = #{id}")
	public Map<String, Object> getKnowledgeDetail(@Param("id")String id);
	
	/**
	 * 插入加盟记录
	 * @param data
	 * @return
	 */
	public int insShopJoin(Map<String, Object> data);
	
	/**
	 * 查询加盟记录
	 * @param status
	 * @param statusin
	 * @return
	 */
	public List<Map<String, Object>> getJoinLog(@Param("userid")String userid,@Param("status")String status,@Param("statusin")String statusin);
	
}


